package com.secondhand.controller;

import com.secondhand.domain.good;
import com.secondhand.domain.user;
import com.secondhand.service.Impl.userServiceImpl;
import com.secondhand.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api("二手交易相关接口")
@Log4j2
@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    userServiceImpl userService;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })
    @GetMapping("/login")
    public AjaxResult login(@RequestParam("username") String username,@RequestParam("password") String password) {

        user result = userService.findByUserNameAndPassword(username,password);
        return AjaxResult.success("用户登陆",result);

    }

    @ApiOperation("用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })
    @PostMapping("/register")
    public AjaxResult register(@RequestParam("username") String username,@RequestParam("password") String password) {
        boolean result = userService.saveUser(username,password);
        return AjaxResult.success("用户注册",result);
    }


    @ApiOperation("用户注销")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @DeleteMapping("/logout")
    public AjaxResult logout(@RequestParam("username") String username){
        Boolean result = userService.deleteUser(username);
        return AjaxResult.success("用户注销",result);
    }

    @ApiOperation("获取用户ID")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @GetMapping("/userId")
    public AjaxResult getUserIdByUsername(@RequestParam("username") String username) {
        int result = userService.getUserIdByUserName(username);
        return AjaxResult.success("用户登陆",result);
    }

    @ApiOperation("用户信息修改")
    @PostMapping("/update")
    public AjaxResult register(@RequestBody user user) {
        System.out.println("用户请求修改信息");
        int result = userService.updateUser(user);
        System.out.println(result);
        return AjaxResult.success("用户信息修改",result);
    }
}
